package com.wl.spring.mvc.web;

import com.wl.spring.mvc.entity.User;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("test")
public class HelloAction {

    @GetMapping(value = "hello", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
    public String hello(String name) {
        System.out.println("=======hello======");
        return "hello" + name;
    }

    @RequestMapping(path = "demo", method = {RequestMethod.POST, RequestMethod.DELETE})
    public String demo(String name) {
        return "demo" + name;
    }

    @GetMapping("get")
    public String get(String name) {
        return "get" + name;
    }

    @PostMapping("post")
    public String post(String name) {
        return "post" + name;
    }

    @GetMapping(path = "param", params = {"a", "b=1", "c!=2"})
    public String param(String name) {
        return "param" + name;
    }

    @RequestMapping(path = "header", headers = {"connection != keep-alive"})
    public String header(String name) {
        return "header" + name;
    }

    @GetMapping("add")
    public int add(int a, int b) {
        return a + b;
    }

    @GetMapping("servlet")
    public Map<String, Object> servlet(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("request", request);
        ret.put("response", response);
        ret.put("session", session);
        return ret;
    }

    @GetMapping("user")
    public User user(User user){
        user.setAge(user.getAge() + 10);
        return user;
    }

    @GetMapping("sub/{c}")
    public int sub(@RequestParam("a") int x,
                   @RequestParam(name = "b",required = false,
                   defaultValue = "10") int y,
                   @PathVariable(value = "c") int z){
        return x - y - z;
    }

    @GetMapping(value = "headers")
    public String headers(
            @RequestHeader String Accept,
            @RequestHeader ("Host") String host,
            @RequestHeader("Cookie") String cookie,
            @CookieValue String name,
            @CookieValue("myAge") String age
    ){
        return String.format("<h1>%s</h1>" +
                "<h1>%s</h1>" +
                "<h1>%s</h1>" +
                "<h1>%s</h1>" +
                "<h1>%s</h1>" ,
                Accept,
                host,
                cookie,name,age);
    }

    @GetMapping("toTest")
    public ModelAndView toTest(ModelAndView mav){
        mav.addObject("myname","李四");
        mav.setViewName("/test.jsp");  //必须使用绝对路径
        return mav;
    }

    @GetMapping("toPay")
    ModelAndView toPay(ModelAndView mav){
        mav.setViewName("redirect:/spring_war_exploded/pay.html");
        return mav;
    }

}
